package com.yy.market.mvp.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sunfusheng.marqueeview.MarqueeView;
import com.yy.market.R;
import com.yy.market.mvp.bean.HomeTypeButton;
import com.yy.market.mvp.data.HomeData;
import com.yy.market.utils.ListUtils;
import com.yy.market.utils.ToolsUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luweibin on 2016/8/16.
 */
public class HomeAdapter extends RecyclerView.Adapter {
    private List<HomeData> homeDatas;
    private Context context;

    public HomeAdapter(ArrayList<HomeData> homeDatas, Context context) {
        this.homeDatas = homeDatas;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HomeData.HOME_TYPE_10_BUTTON) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_item_10_button, parent, false);
            return new TenButtonViewHolder(view,context);
        } else if (viewType == HomeData.HOME_TYPE_SPEC) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_item_spec, parent, false);
            return new SpecViewHolder(view);
        } else if (viewType == HomeData.HOME_TYPE_AD_VIEW) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_item_ad_view, parent, false);
            return new AdViewHolder(view);
        }

        else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_item, parent, false);
            return new DefaultViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TenButtonViewHolder) {
            TenButtonViewHolder tenButtonViewHolder = (TenButtonViewHolder) holder;
            List<HomeTypeButton> homeTypeButtons = (List<HomeTypeButton>)(homeDatas.get(position).getData());
            tenButtonViewHolder.recyclerView.setAdapter(new Home10BtnAdapter(context,homeTypeButtons));
        } else if (holder instanceof AdViewHolder) {
            AdViewHolder adViewHolder = (AdViewHolder) holder;
            List<String> ads = (List<String>)(homeDatas.get(position).getData());
            adViewHolder.marqueeView.startWithList(ads);
        } else if (holder instanceof SpecViewHolder) {
            SpecViewHolder specViewHolder = (SpecViewHolder) holder;
            Object height = homeDatas.get(position).getData();
            float f = 10f;
            if (height != null) {
                f = (float) height;
            }
            specViewHolder.specView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup
                    .LayoutParams.MATCH_PARENT, ToolsUtil.dip2px(context,f)));
        }
    }

    @Override
    public int getItemViewType(int position) {
//        if (position <  ListUtils.getSize(homeDatas) && position == 0) {
        if (position < getItemCount()) {
            return homeDatas.get(position).getType();
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return ListUtils.getSize(homeDatas);
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder {
        public DefaultViewHolder(View itemView) {
            super(itemView);
        }
    }

    static class SpecViewHolder extends RecyclerView.ViewHolder {
        View specView;
        public SpecViewHolder(View itemView) {
            super(itemView);
            specView = itemView.findViewById(R.id.home_spec_view);
        }
    }
    static class AdViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        MarqueeView marqueeView;
        public AdViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.home_ad_imageView);
            marqueeView = (MarqueeView) itemView.findViewById(R.id.home_ad_marqueeView);
        }
    }

    static class TenButtonViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;
        public TenButtonViewHolder(View itemView,Context context) {
            super(itemView);
            recyclerView = (RecyclerView) itemView;
            recyclerView.setLayoutManager(new GridLayoutManager(context,5));
        }
    }

}
